#!/usr/bin/env python

import sys
sys.path.insert(0, '../') #　../当前目录的上一级目录，也就是　base_dir


#告诉os我们的django配置文件在哪里
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "meiduo_mall.settings")


import django
django.setup()


from apps.goods.models import SKU
from utils.goods import get_categories
from utils.goods import get_breadcrumb
from utils.goods import get_goods_specs

def generic_detail_html(sku):
    # １．分类数据
    categories = get_categories()

    # ２．面包屑
    breadcrumb = get_breadcrumb(sku.category)
    # ３．SKU信息
    # ４．规格信息
    goods_specs = get_goods_specs(sku)

    context = {
        'categories': categories,
        'breadcrumb': breadcrumb,
        'sku': sku,
        'specs': goods_specs
    }

    # １．加载渲的模板
    from django.template import loader
    detail_template = loader.get_template('detail.html')

    # ２．把数据给模板
    detail_html_data = detail_template.render(context)

    # ３．把渲染好的ＨＴＭＬ写入指定文件
    from meiduo_mall import settings
    import os
    file_path = os.path.join(os.path.dirname(settings.BASE_DIR), 'front_end_pc/goods/%s.html' % sku.id)
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(detail_html_data)

    print(sku.id)

skus = SKU.objects.all()
for sku in skus:
    generic_detail_html(sku)